import pygame, sys, math

class Player():
    def __init__(self, image, speed = [2,2], pos = (0,0)):
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.maxSpeedx = speed[0]
        self.maxSpeedy = speed[1]
        self.speedx = 0
        self.speedy = 0
        self.speed = [self.speedx, self.speedy]
        self.radius = self.rect.width/2
        self.place(pos)
        self.angle = 0
        self.living = True
        
    def place(self, pos):
        self.rect.center = pos
        
    def direction(self, dir):
        
        if dir == "right":
            self.speedx = self.maxSpeedx
            self.angle = 0
        if dir == "stop right":
            self.speedx = 0
        if dir == "left":
            self.speedx = -self.maxSpeedx
            self.angle = 180
            print self.angle
        if dir == "stop left":
            self.speedx = 0
        if dir == "up":
            self.speedy = -self.maxSpeedy
            self.angle = 90
        if dir == "stop up":
            self.speedy = 0
        if dir == "down":
            self.speedy = self.maxSpeedy
            self.angle = -90
        if dir == "stop down":
            self.speedy = 0
    
    def update(self):
        self.move()
    
    def animate(self):
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
            self.image = self.images[self.frame]
    
    def move(self):
        self.speed = [self.speedx, self.speedy]
        self.rect = self.rect.move(self.speed)
        
    def collideWall(self, width, height):
        if self.rect.left < -(self.rect.width):
            self.place([width + self.rect.width/2, self.rect.center[1]])
        elif self.rect.right > width + self.rect.width:
            self.place([-self.rect.width/2, self.rect.center[1]])
        if self.rect.top < -(self.rect.height):
            self.place([self.rect.center[0], self.rect.height/2 + height])
        elif self.rect.bottom > height + self.rect.height:
            self.place([self.rect.center[0], -self.rect.height/2])
        
        
        
    def collideAnimal(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    self.living = False
                    print self.living
    
    def distanceToPoint(self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[1]
        x2 = pt[0]
        y2 = pt[1]
        
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
    